package com.zoffy.wmsbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zoffy.wmsbackend.common.BaseResponse;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.common.ResultUtils;
import com.zoffy.wmsbackend.mapper.WarehouseMapper;
import com.zoffy.wmsbackend.model.dto.warehouse.WareHouseDTO;
import com.zoffy.wmsbackend.model.entity.Warehouse;
import com.zoffy.wmsbackend.model.vo.WarehouseVO;
import com.zoffy.wmsbackend.service.WarehouseService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@PreAuthorize("hasAuthority('admin')")
@RequestMapping("/warehouse")
public class WareHouseController {
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private WarehouseMapper warehouseMapper;
    @PostMapping("/list")
    public BaseResponse<IPage<WarehouseVO>> list(@RequestBody RequestParam<WareHouseDTO> requestParam){
        IPage<WarehouseVO> res = warehouseService.listWarehousePage(requestParam);
        return ResultUtils.success(res);
    }
    @PostMapping("/add")
    public BaseResponse<Boolean> addWarehouse(@RequestBody WareHouseDTO dto){
        Boolean res = warehouseService.addWarehouse(dto);
        return ResultUtils.success(res);
    }
    @PostMapping("/update")
    public BaseResponse<Boolean> updateWarehouse(@RequestBody WareHouseDTO dto){
        Boolean res = warehouseService.updateWarehouse(dto);
        return ResultUtils.success(res);
    }
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteWarehouse(@RequestBody WareHouseDTO dto){
        Boolean res = warehouseService.deleteWarehouse(dto);
        return ResultUtils.success(res);
    }
    @GetMapping("/listAll")
    public BaseResponse<List<WarehouseVO>> listAll(){
        List<Warehouse> ls = warehouseService.list(new QueryWrapper<>());
        List<WarehouseVO> res = new ArrayList<>();
        for(Warehouse w : ls){
            res.add(WarehouseVO.obj2vo(w));
        }
        return ResultUtils.success(res);
    }
    @GetMapping("/test")
    public BaseResponse testWarehouse(@RequestBody WareHouseDTO dto){
        List<Long> warehouseIds =new ArrayList<>();
        warehouseIds.add(1L);
        warehouseIds.add(2L);
        warehouseIds.add(3L);
        warehouseMapper.getWarehouseByIds(warehouseIds,"xx");
        return ResultUtils.success();
    }
}
